package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.common.ObjectUtil;
import com.shop.common.result.Result;
import com.shop.common.result.util.ResultModel;
import com.shop.entity.PartnerDo;
import com.shop.mapper.PartnerDoMapper;
import com.shop.service.PartnerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.vo.AddPartnerVo;
import com.shop.vo.SelectPartnerVo;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 合作商表 服务实现类
 * </p>
 *
 * @author 靳 旺
 * @since 2021-01-29
 */
@Service
public class PartnerServiceImpl extends ServiceImpl<PartnerDoMapper, PartnerDo> implements PartnerService {

    @Override
    public ResultModel addPartner(AddPartnerVo addPartnerVo) throws Exception {
        PartnerDo partnerDo = new PartnerDo();
        ObjectUtil.annotationToObject(addPartnerVo, partnerDo);
        if (!super.save(partnerDo)) throw new Exception("保存合作商信息失败！");
        return Result.successResultModel();
    }

    @Override
    public ResultModel addBatchPartner(List<AddPartnerVo> addPartnerVo) throws Exception {
        if (addPartnerVo.size() <= 0) throw new Exception("合作商不能为空！");
        List<PartnerDo> partnerDos = new ArrayList<>();
        addPartnerVo.stream().forEach(k -> {
            PartnerDo partnerDo = new PartnerDo();
            ObjectUtil.annotationToObject(k, partnerDo);
            partnerDos.add(partnerDo);
        });
        if (!super.saveBatch(partnerDos)) throw new Exception("保存合作商信息失败！");
        return Result.successResultModel();
    }

    @Override
    public ResultModel selectPartner(SelectPartnerVo selectPartnerVo) throws Exception {
        PartnerDo partnerDo = new PartnerDo();
        ObjectUtil.annotationToObject(selectPartnerVo, partnerDo);
        partnerDo.setIsDel("n");
        QueryWrapper<PartnerDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", partnerDo.getIsDel());
        List<PartnerDo> partnerDos = super.list(queryWrapper);
        Collections.sort(partnerDos, (s1, s2) -> s1.getSort().compareTo(s2.getSort()));
        return Result.successResultModel(partnerDos);
    }


}
